<!--
 * @Descripttion: 编辑商品
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2022-02-12 14:55:18
-->
<template>
  <div class="lb-goods-edit">
    <top-nav :title="navTitle" :isBack="true"></top-nav>
    <div class="page-main">
      <lb-classify-title title="基本信息"></lb-classify-title>
      <el-form
        @submit.native.prevent
        :model="basicform"
        :rules="basicFormRules"
        ref="basicform"
        label-width="180px"
        class="basic-form"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input
            type="text"
            v-model="basicform.name"
            placeholder="请输入商品名称"
            maxlength="38"
            show-word-limit
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="商品副标" prop="s_title">
          <el-input v-model="basicform.s_title" placeholder="请输入商品副标题"></el-input>
        </el-form-item> -->
        <el-form-item label="选择商品分类" prop="type">
          <el-cascader
            v-model="basicform.type"
            :options="options.cate"
            :props="defaultParams"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="选择公司" prop="company_id">
          <el-cascader
            v-model="basicform.company_id"
            collapse-tags
            :options="options.company"
            @change="choiceAllCompanyItem"
            :props="{
              multiple: true,
              checkStrictly: true,
              label: 'name',
              value: 'id'
            }"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>

      <lb-classify-title title="规格信息"></lb-classify-title>
      <div class="specs-info">
        <div class="specs-item">
          <div class="item-label">
            <span class="require">*</span>
            <span>创建商品规格</span>
          </div>
          <div class="item-value">
            <lb-button
              type="primary"
              @click="addOrEditSpecsTitle('add')"
              plain
              icon="el-icon-plus"
              >添加规格</lb-button
            >
          </div>
        </div>
        <!-- 规格*START -->
        <div
          class="specs-item"
          v-for="(item, index) in specsForm.specsItem"
          :key="index"
        >
          <div class="item-label"></div>
          <div class="item-value">
            <div class="item-value-specs-name">
              <span>规格名： {{ item.title }}</span>
              <lb-button
                type="warning"
                size="mini"
                plain
                icon="el-icon-edit"
                @click="addOrEditSpecsTitle('edit', index)"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                type="danger"
                size="mini"
                plain
                icon="el-icon-delete"
                @click="delSpecsItem(index)"
                >{{ $t('action.delete') }}</lb-button
              >
              <lb-button
                :type="
                  !specIsImg
                    ? 'primary'
                    : item.is_img === 1
                    ? 'primary'
                    : 'info'
                "
                size="mini"
                plain
                :icon="
                  !specIsImg
                    ? 'el-icon-circle-plus'
                    : item.is_img === 1
                    ? 'el-icon-remove'
                    : 'el-icon-circle-plus'
                "
                @click="setSpecImg(index)"
                >{{ $t('action.addImage') }}</lb-button
              >
            </div>
            <div class="flex-warp">
              <div
                v-for="(tag, i) in item.cate"
                :key="i"
                class="specsItem-tag-img"
              >
                <el-tag
                  closable
                  size="medium"
                  :disable-transitions="false"
                  effect="plain"
                  type="success"
                  @close="handleCloseTag(index, i)"
                  >{{ tag.title }}</el-tag
                >
                <div v-if="item.is_img === 1" style="margin-top: 5px">
                  <lb-cover
                    :fileList="tag.image"
                    size="small"
                    @selectedFiles="getSpecImg($event, index, i)"
                  ></lb-cover>
                </div>
              </div>

              <div class="specsItem-tag-img">
                <el-input
                  type="textarea"
                  :rows="2"
                  resize="none"
                  maxlength="50"
                  show-word-limit
                  v-show="item.inputVisible"
                  v-model="item.inputValue"
                  :ref="'saveTagInput' + index"
                  style="width: 300px"
                  @keyup.enter.native="handleInputConfirm(index)"
                  @blur="handleInputConfirm(index)"
                ></el-input>
                <el-button
                  v-show="!item.inputVisible"
                  class="button-new-tag"
                  size="mini"
                  @click="showInput(index)"
                  style="margin-top: 5px"
                >
                  + 添加规格值
                </el-button>
              </div>
            </div>
          </div>
        </div>
        <!-- 添加规格*END -->
        <div class="specs-item">
          <div class="item-label item-label-table">
            <!-- <span class="require">*</span> -->
            <span>编辑规格</span>
          </div>
          <div class="item-value">
            <el-table
              ref="multipleTable"
              :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
              tooltip-effect="dark"
              @selection-change="handleSelectionChange"
              :data="specsForm.specsTable"
              style="width: 800px"
            >
              <el-table-column
                type="selection"
                label="全选"
                width="45"
              ></el-table-column>
              <el-table-column
                prop="title"
                label="规格组合名称"
              ></el-table-column>
              <el-table-column prop="original_price" label="原价" width="140">
                <template slot-scope="scope">
                  <el-input-number
                    size="mini"
                    v-model="scope.row.original_price"
                    :controls="false"
                    :precision="2"
                    :min="0.0"
                    :max="999999999.99"
                  ></el-input-number>
                </template>
              </el-table-column>
              <el-table-column prop="price" label="现价" width="140">
                <template slot-scope="scope">
                  <el-input-number
                    size="mini"
                    v-model="scope.row.price"
                    :controls="false"
                    :precision="2"
                    :min="0.0"
                    :max="999999999.99"
                  ></el-input-number>
                </template>
              </el-table-column>
              <el-table-column prop="cost_price" label="成本" width="140">
                <template slot-scope="scope">
                  <el-input-number
                    size="mini"
                    v-model="scope.row.cost_price"
                    :controls="false"
                    :precision="2"
                    :min="0.0"
                    :max="999999999.99"
                  ></el-input-number>
                </template>
              </el-table-column>
              <el-table-column prop="stock" label="库存" width="140">
                <template slot-scope="scope">
                  <el-input-number
                    size="mini"
                    v-model="scope.row.stock"
                    :controls="false"
                    :precision="0"
                    :min="0"
                  ></el-input-number>
                </template>
              </el-table-column>
            </el-table>
            <div class="table-bot">
              <span>已选 {{ specsForm.multipleSelection.length }}</span>
              <span>批量设置：</span>
              <el-link
                :disabled="specsForm.multipleSelection.length === 0"
                type="primary"
                @click="batchSetting('原价')"
                >原价</el-link
              >
              <el-link
                :disabled="specsForm.multipleSelection.length === 0"
                type="primary"
                @click="batchSetting('现价')"
                >现价</el-link
              >
              <el-link
                :disabled="specsForm.multipleSelection.length === 0"
                type="primary"
                @click="batchSetting('成本')"
                >成本</el-link
              >
              <el-link
                :disabled="specsForm.multipleSelection.length === 0"
                type="primary"
                @click="batchSetting('库存')"
                >库存</el-link
              >
            </div>
          </div>
        </div>
      </div>

      <lb-classify-title
        title="相关信息"
        tips="注：当未设置商品规格的时候小程序将会显示设置的默认价格。"
      ></lb-classify-title>
      <el-form
        @submit.native.prevent
        :model="relevantForm"
        :rules="relevantFormRules"
        ref="relevantForm"
        label-width="120px"
        :inline="true"
        class="relevant-form"
        style="padding-left: 60px"
      >
        <el-form-item label="单位" prop="unit">
          <el-input
            v-model="relevantForm.unit"
            placeholder="请输入单位"
          ></el-input>
          <lb-tool-tips>商品单位，如：个、件、瓶、套等</lb-tool-tips>
        </el-form-item>
        <el-form-item label="运费" prop="freight">
          <el-input-number
            class="lb-input-number"
            v-model="relevantForm.freight"
            :controls="false"
            :precision="2"
            :min="0"
            placeholder="请输入运费"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="浏览量" prop="view_count">
          <el-input
            v-model.number="relevantForm.view_count"
            placeholder="请输入浏览量"
          ></el-input>
          <lb-tool-tips>商品虚拟浏览量</lb-tool-tips>
        </el-form-item>
        <el-form-item label="销售量" prop="sale_count">
          <el-input
            v-model.number="relevantForm.sale_count"
            placeholder="请输入销售量"
          ></el-input>
          <lb-tool-tips
            >商品虚拟销售量，用于小程序展示给用户看，用户购买商品后会累加相应购买数量</lb-tool-tips
          >
        </el-form-item>
        <el-form-item label="排序值" prop="top">
          <el-input-number
            class="lb-input-number"
            v-model="relevantForm.top"
            :controls="false"
            :precision="0"
            :min="0"
            placeholder="请输入排序值"
          ></el-input-number>
          <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
        </el-form-item>
      </el-form>
      <!-- 会员信息 -->
      <lb-classify-title
        title="会员信息"
        v-if="isMemberAuth === true"
      ></lb-classify-title>
      <el-form
        @submit.native.prevent
        :model="memberForm"
        :rules="memberFormRules"
        ref="memberForm"
        label-width="180px"
        class="set-form"
        v-if="isMemberAuth === true"
      >
        <el-form-item label="会员升级专属商品" prop="is_member">
          <el-radio-group v-model="memberForm.is_member">
            <el-radio :label="0">关闭</el-radio>
            <el-radio :label="1">开启</el-radio>
          </el-radio-group>
          <lb-tool-tips
            >如若开启，则此商品不展示在正常商城中，作为会员升级专属商品</lb-tool-tips
          >
        </el-form-item>
        <el-form-item label="会员折扣" prop="is_discount">
          <el-radio-group v-model="memberForm.is_discount">
            <el-radio :label="0">关闭</el-radio>
            <el-radio :label="1">开启</el-radio>
          </el-radio-group>
          <lb-tool-tips v-if="memberForm.is_discount === 1"
            >会员折扣取值范围为0-10，最多保留1位小数，不输入将默认为10（0：打全折；10：不打折）</lb-tool-tips
          >
        </el-form-item>
      </el-form>
      <el-form
        label-width="180px"
        style="padding-left: 180px; width: 910px"
        :inline="true"
        v-if="memberForm.is_discount === 1 && levelList.length > 0"
      >
        <el-form-item
          label=""
          prop=""
          v-for="(item, index) in levelList"
          :key="index"
        >
          <el-input
            :class="[
              index === 1 || index === 4 || index === 7 ? 'ml-lg mr-lg' : ''
            ]"
            v-model="item.discount"
            style="width: 280px"
          >
            <template slot="prepend">VIP{{ item.top }}</template>
            <template slot="append">折</template>
          </el-input>
        </el-form-item>
      </el-form>

      <lb-classify-title title="参数信息"></lb-classify-title>
      <el-form label-width="180px" class="relevant-form" style="padding: 0">
        <el-form-item label="参数信息" prop="price">
          <el-radio-group v-model="basicform.is_parameter">
            <el-radio :label="0">关闭</el-radio>
            <el-radio :label="1">开启</el-radio>
          </el-radio-group>
          <el-select
            @change="changeParmar"
            v-if="basicform.is_parameter === 1"
            v-model="basicform.template_id"
            placeholder="请选择"
          >
            <el-option
              v-for="item in parimarOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form
        label-width="120px"
        class="relevant-form"
        style="
          padding: 10px 0 0 0;
          margin-left: 80px;
          border: 1px solid #eff2f6;
          border-radius: 5px;
        "
        v-if="basicform.is_parameter === 1 && parimarForm.length > 0"
      >
        <div v-for="(firstItem, firstIndex) in parimarForm" :key="firstIndex">
          <el-form-item
            :label="firstItem.parameter_name"
            prop="unit"
          ></el-form-item>
          <el-row class="el-row parmai_view" type="flex">
            <el-col
              :span="8"
              v-for="(item, index) in firstItem.secondList"
              :key="index"
            >
              <el-form-item
                v-if="item.parameter_name"
                :label="item.parameter_name"
                prop="unit"
              >
                <el-input
                  v-model="item.parameter_value"
                  placeholder="请输入参数值"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <div style="height: 20px"></div>
      <lb-classify-title title="商品信息"></lb-classify-title>
      <el-form
        @submit.native.prevent
        :model="imgForm"
        :rules="imgFormRules"
        ref="imgForm"
        label-width="180px"
        class="img-form"
      >
        <el-form-item label="封面图" prop="cover">
          <lb-cover
            :fileList="imgForm.cover"
            @selectedFiles="getGoodsCover"
          ></lb-cover>
          <lb-tool-tips>图片建议尺寸: 750 * 750</lb-tool-tips>
        </el-form-item>
        <el-form-item label="轮播图" prop="images">
          <lb-cover
            type="more"
            :fileList="imgForm.images"
            @selectedFiles="selectedFiles($event, 'images')"
            @moveFiles="moveFiles($event, 'images')"
          ></lb-cover>
          <lb-tool-tips>图片建议尺寸：750 * 750</lb-tool-tips>
        </el-form-item>
        <el-form-item label="轮播图链接" prop="image_url">
          <el-input
            v-model="imgForm.image_url"
            placeholder="请输入轮播图跳转链接"
          ></el-input>
          <lb-tool-tips>点击轮播图跳转网址</lb-tool-tips>
        </el-form-item>
        <el-form-item label="视频" prop="video">
          <div class="item-warp">
            <div class="upload-file-warp">
              <input
                type="text"
                class="choice-file-input"
                v-model="imgForm.video"
                placeholder="选择视频文件"
              />
              <lb-cover
                type="button"
                fileType="video"
                @selectedFiles="getVideo"
              ></lb-cover>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="商品详情" prop="content">
          <lb-ueditor v-model="imgForm.content" :destroy="true"></lb-ueditor>
        </el-form-item>
      </el-form>

      <lb-classify-title title="相关设置"></lb-classify-title>
      <el-form
        @submit.native.prevent
        :model="setForm"
        :rules="setFormRules"
        ref="setForm"
        label-width="180px"
        class="set-form"
      >
        <!-- <el-form :inline="true" class="relevant-form"> -->
        <el-form-item label="是否限购" prop="is_limit">
          <el-radio-group v-model="setForm.is_limit">
            <el-radio :label="2">{{ $t('是') }}</el-radio>
            <el-radio :label="1">{{ $t('否') }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="setForm.is_limit === 2"
          label="限购数量"
          prop="limit"
        >
          <el-input-number
            class="lb-input-number"
            v-model="setForm.limit"
            :controls="false"
            :precision="0"
            :min="1"
            placeholder="请输入限购数量"
          ></el-input-number>
          <lb-tool-tips>限制每人可购买数量，仅限制该商品</lb-tool-tips>
        </el-form-item>
        <!-- </el-form> -->

        <el-form-item label="起购数量" prop="start_min">
          <!-- <el-input v-model="setForm.start_min" placeholder="请输入内容"></el-input> -->
          <el-input-number
            class="lb-input-number"
            v-model.number="setForm.start_min"
            :controls="false"
            :precision="0"
            :min="1"
            placeholder="请输入起购数量"
          ></el-input-number>
          <lb-tool-tips
            >设置之后，买家需要达到起购数才能下单成功，起售数量超出商品库存时，买家无法购买该商品。</lb-tool-tips
          >
        </el-form-item>
        <el-form-item label="是否价格面议" prop="price">
          <el-radio-group v-model="setForm.price">
            <el-radio :label="0">{{ $t('action.ON') }}</el-radio>
            <el-radio :label="1">{{ $t('action.OFF') }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否设为推荐" prop="recommend">
          <el-radio-group v-model="setForm.recommend">
            <el-radio :label="1">{{ $t('action.ON') }}</el-radio>
            <el-radio :label="0">{{ $t('action.OFF') }}</el-radio>
          </el-radio-group>
          <lb-tool-tips>设为推荐则该商品会在所属商品分类中置顶</lb-tool-tips>
        </el-form-item>
        <el-form-item label="是否设为首页推荐" prop="index_show">
          <el-radio-group v-model="setForm.index_show">
            <el-radio :label="1">{{ $t('action.ON') }}</el-radio>
            <el-radio :label="0">{{ $t('action.OFF') }}</el-radio>
          </el-radio-group>
          <lb-tool-tips>开启首页推荐，商品才会展示在商城首页</lb-tool-tips>
        </el-form-item>

        <el-form
          @submit.native.prevent
          :model="sendForm"
          :rules="sendFormRules"
          ref="sendForm"
          label-width="180px"
          class="basic-form"
        >
          <el-form-item label="配送方式" prop="checkList">
            <el-checkbox-group
              v-model="sendForm.checkList"
              @change="changeCheckBox"
            >
              <div
                v-for="(item, index) in authList"
                :key="index"
                :style="{
                  display: 'inline-block',
                  marginLeft: index === 0 ? 0 : '15px'
                }"
              >
                <el-checkbox :label="item.title"></el-checkbox>
                <lb-tool-tips v-if="item.tips">{{ item.tips }}</lb-tool-tips>
              </div>

              <lb-tool-tips
                >自提商品不需要发货, 后台可以直接完成提货订单</lb-tool-tips
              >
            </el-checkbox-group>

            <!-- <el-radio-group v-model="setForm.is_self">
            <el-radio :label="1">{{$t('action.ON')}}</el-radio>
            <el-radio :label="0">{{$t('action.OFF')}}</el-radio>
          </el-radio-group> -->
          </el-form-item>
        </el-form>
        <el-form-item label="用户分销提成" prop="switch">
          <el-radio-group v-model="setForm.switch">
            <el-radio :label="0">关闭分销</el-radio>
            <el-radio
              :label="3"
              v-if="
                (distribution.selling_style === 1 &&
                  distribution.first_extract) ||
                (distribution.selling_style === 2 &&
                  distribution.first_extract_cash)
              "
              >使用全局用户分销提成</el-radio
            >
            <el-radio :label="1">自定义百分比</el-radio>
            <el-radio :label="2">自定义金额</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label
          prop="staff_extract"
          v-if="setForm.switch === 3 && distribution.selling_style === 1"
        >
          <el-input-number
            class="lb-input-number"
            v-model="distribution.first_extract"
            :disabled="true"
            :controls="false"
            :precision="0"
          ></el-input-number>
          <block>%</block>
          <lb-tool-tips>{{
            tips.user[distribution.selling_style]
          }}</lb-tool-tips>
        </el-form-item>
        <el-form-item
          label
          prop="staff_extract"
          v-if="setForm.switch === 3 && distribution.selling_style === 2"
        >
          <el-input-number
            class="lb-input-number"
            v-model="distribution.first_extract_cash"
            :disabled="true"
            :controls="false"
            :precision="0"
          ></el-input-number>
          <block>¥</block>
          <lb-tool-tips>{{
            tips.user[distribution.selling_style] + tips.cash_1
          }}</lb-tool-tips>
        </el-form-item>
        <el-form-item label prop="extract" v-if="setForm.switch === 1">
          <el-input-number
            class="lb-input-number"
            v-model.number="setForm.extract"
            :controls="false"
            :precision="0"
            :min="1"
            :max="100"
          ></el-input-number>
          <block>%</block>
          <lb-tool-tips>{{ tips.user[setForm.switch] }}</lb-tool-tips>
        </el-form-item>
        <el-form-item label prop="extract_cash" v-if="setForm.switch === 2">
          <el-input-number
            class="lb-input-number"
            v-model.number="setForm.extract_cash"
            :controls="false"
            :precision="2"
            :min="0"
          ></el-input-number>
          <block>¥</block>
          <lb-tool-tips>{{
            tips.user[setForm.switch] + tips.cash_2
          }}</lb-tool-tips>
        </el-form-item>

        <el-form-item label="员工分销提成" prop="staff_switch">
          <el-radio-group v-model="setForm.staff_switch">
            <el-radio :label="0">关闭分销</el-radio>
            <el-radio
              :label="3"
              v-if="
                (distribution.selling_style === 1 &&
                  distribution.staff_extract) ||
                (distribution.selling_style === 2 &&
                  distribution.staff_extract_cash)
              "
              >使用全局员工分销提成</el-radio
            >
            <el-radio :label="1">自定义百分比</el-radio>
            <el-radio :label="2">自定义金额</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          label
          prop="staff_extract"
          v-if="setForm.staff_switch === 3 && distribution.selling_style === 1"
        >
          <el-input-number
            class="lb-input-number"
            v-model="distribution.staff_extract"
            :disabled="true"
            :controls="false"
            :precision="0"
          ></el-input-number>
          <block>%</block>
          <lb-tool-tips>{{
            tips.staff[distribution.selling_style]
          }}</lb-tool-tips>
        </el-form-item>
        <el-form-item
          label
          prop="staff_extract"
          v-if="setForm.staff_switch === 3 && distribution.selling_style === 2"
        >
          <el-input-number
            class="lb-input-number"
            v-model="distribution.staff_extract_cash"
            :disabled="true"
            :controls="false"
            :precision="0"
          ></el-input-number>
          <block>¥</block>
          <lb-tool-tips>{{
            tips.staff[distribution.selling_style] + tips.cash_1
          }}</lb-tool-tips>
        </el-form-item>
        <el-form-item
          label
          prop="staff_extract"
          v-if="setForm.staff_switch === 1"
        >
          <el-input-number
            class="lb-input-number"
            v-model.number="setForm.staff_extract"
            :controls="false"
            :precision="0"
            :min="1"
            :max="100"
          ></el-input-number>
          <block>%</block>
          <lb-tool-tips>{{ tips.staff[setForm.staff_switch] }}</lb-tool-tips>
        </el-form-item>
        <el-form-item
          label
          prop="staff_extract_cash"
          v-if="setForm.staff_switch === 2"
        >
          <el-input-number
            class="lb-input-number"
            v-model.number="setForm.staff_extract_cash"
            :controls="false"
            :precision="2"
            :min="0"
          ></el-input-number>
          <block>¥</block>
          <lb-tool-tips>{{
            tips.staff[setForm.staff_switch] + tips.cash_2
          }}</lb-tool-tips>
        </el-form-item>
      </el-form>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogSwitch"
      width="400px"
      @keyup.enter.native="addSpecsName(2)"
      center
    >
      <div class="dialog-inner">
        <el-input
          type="textarea"
          :rows="4"
          resize="none"
          maxlength="50"
          show-word-limit
          v-model="specsName"
          placeholder="请输入规格名"
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addSpecsName(1)">{{
          $t('action.cancel')
        }}</el-button>
        <el-button type="primary" @click="addSpecsName(2)">{{
          $t('action.comfirm')
        }}</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="`批量设置${dialogPriceTitle}`"
      :visible.sync="dialogPriceVisible"
      width="300px"
      @keyup.enter.native="setBatchPrice(2)"
      center
    >
      <div class="dialog-inner">
        <el-input-number
          v-if="dialogPriceTitle === '库存'"
          v-model="batchValue"
          :controls="false"
          :precision="0"
          :min="0"
        ></el-input-number>
        <el-input-number
          v-else
          v-model="batchValue"
          :controls="false"
          :precision="2"
          :min="0.0"
          :max="999999999.99"
        ></el-input-number>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setBatchPrice(1)">{{
          $t('action.cancel')
        }}</el-button>
        <el-button type="primary" @click="setBatchPrice(2)">{{
          $t('action.comfirm')
        }}</el-button>
      </span>
    </el-dialog>

    <div
      class="fiexd"
      style="bottom: 0px"
      :style="{ right: adSwitch ? '242px' : '22px' }"
    >
      <el-button type="primary" @click="submitGoodsInfo">{{
        $t('action.submit')
      }}</el-button>
      <el-button type="default" style="margin-left: 10px" @click="goBack">{{
        $t('action.back')
      }}</el-button>
    </div>
  </div>
</template>

<script>
// import {reg} from '@/utils/reg'
import Uuid from 'uuid'
import { mapGetters } from 'vuex'
export default {
  data () {
    let validateLimit = (rule, value, callback) => {
      if (this.setForm.is_limit === 2) {
        if (!value || value.length === 0) {
          callback(new Error('请输入限购数量'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    // let checkNumber1 = (rule, value, callback) => {
    //   if (this.setForm.switch === 1) {
    //     if (!/^(?:0|[1-9][0-9]?|100)$/.test(value)) {
    //       callback(new Error('请输入0-100的整数'))
    //     } else {
    //       callback()
    //     }
    //   }
    // }
    // let checkNumber2 = (rule, value, callback) => {
    //   if (this.setForm.staff_switch === 1) {
    //     if (!/^(?:0|[1-9][0-9]?|100)$/.test(value)) {
    //       callback(new Error('请输入0-100的整数'))
    //     } else {
    //       callback()
    //     }
    //   }
    // }
    return {
      parimarForm: [], // 参数表单
      parimarOptions: [], // 下拉参数列表
      distribution: {},
      tips: {
        cash_1: '，请谨慎选择',
        cash_2: '，请谨慎输入此金额',
        user: {
          1: '用户消费, 其上线所得提成，无上线时无提成。提成为百分比，取值1%到100%',
          2: '用户消费, 其上线所得提成，无上线时无提成。提成为固定金额，最多保留两位小数，若输入的金额大于所购买规格的商品，也是按照输入的金额来进行提成'
        },
        staff: {
          1: `用户消费, 其对应员工所得提成。当消费的用户的上线与消费所属${this.$t(
            'menu.BusinessCard'
          )}为同一个人时, 只计算员工提成, 如果没有设置员工提成则计算分销提成, 都没有设置则不计算。提成为百分比，取值1%到100%`,
          2: `用户消费, 其对应员工所得提成。当消费的用户的上线与消费所属${this.$t(
            'menu.BusinessCard'
          )}为同一个人时, 只计算员工提成, 如果没有设置员工提成则计算分销提成, 都没有设置则不计算。提成为固定金额，最多保留两位小数，若输入的金额大于所购买规格的商品，也是按照输入的金额来进行提成`
        }
      },
      testprice: 1,
      testContent: '测试',
      dialogSwitch: false,
      dialogTitle: '添加规格',
      dialogType: 'add',
      dialogIndex: '',
      specsName: '',
      batchValue: '0',
      dialogPriceTitle: '',
      dialogPriceVisible: false,
      id: '',
      navTitle: '',
      copySpecsItem: [], // 备份规格表格值
      // 树状单选下拉框配置
      defaultParams: {
        checkStrictly: true,
        label: 'title',
        value: 'id'
      },
      parForm: {},
      // 基本信息表单值
      basicform: {
        template_id: '',
        is_parameter: 0,
        name: '',
        s_title: '',
        type: [],
        company_id: [],
        copyCompanyId: []
      },
      // 基本信息验证规则
      basicFormRules: {
        name: {
          required: true,
          type: 'string',
          message: '请输入商品名称',
          trigger: 'blur'
        },
        // s_title: {
        //   required: true,
        //   type: 'string',
        //   message: '请输入商品副标题',
        //   trigger: 'blur'
        // },
        type: {
          required: true,
          type: 'array',
          message: '请选择商品分类',
          trigger: 'change'
        },
        company_id: {
          required: true,
          type: 'array',
          message: '请选择公司',
          trigger: 'change'
        }
      },
      specIsImg: false,
      // 规格信息表单值
      specsForm: {
        specsItem: [],
        specsTable: [], // pid
        multipleSelection: []
      },
      // 相关信息表单值
      relevantForm: {
        unit: '',
        freight: '',
        view_count: '',
        sale_count: '',
        top: ''
      },
      // 相关信息验证规则
      relevantFormRules: {
        // stock: {required: true, type: 'number', message: '请输入总库存为数字', trigger: 'blur'},
        unit: {
          required: true,
          message: '请输入单位',
          trigger: 'blur'
        },
        freight: {
          required: true,
          type: 'number',
          message: '请输入运费为数字',
          trigger: 'blur'
        },
        view_count: {
          required: true,
          type: 'number',
          message: '请输入浏览量为数字',
          trigger: 'blur'
        },
        sale_count: {
          required: true,
          type: 'number',
          message: '请输入销售量为数字',
          trigger: 'blur'
        },
        top: {
          required: true,
          type: 'number',
          message: '请输入排序值为数字',
          trigger: 'blur'
        }
      },
      // 图片表单
      imgForm: {
        cover: [],
        images: [],
        image_url: '',
        video: '',
        video_cover: [],
        content: ''
      },
      imgFormRules: {
        cover: {
          required: true,
          type: 'array',
          message: '请上传图片',
          trigger: 'change'
        },
        images: {
          required: true,
          type: 'array',
          message: '请上传图片',
          trigger: 'change'
        },
        image_url: {
          // required: true,
          type: 'string',
          message: '请输入链接地址',
          trigger: 'blur'
        },
        content: {
          required: true,
          type: 'string',
          message: '请输入商品详情',
          trigger: 'blur'
        }
      },
      authList: [
        {
          title: '第三方物流',
          key: 'is_send'
        },
        {
          title: '到店自提',
          key: 'is_self'
        }
      ],
      sendForm: {
        checkList: []
      },
      sendFormRules: {
        checkList: {
          required: true,
          type: 'array',
          message: '请选择配送方式',
          trigger: 'change'
        }
      },
      // 设置信息表单值
      setForm: {
        is_limit: 2,
        limit: '',
        start_min: '',
        price: 1,
        recommend: 0,
        index_show: 0,
        is_send: 0,
        is_self: 0,
        switch: 0,
        extract: 1,
        extract_cash: 0.1,
        staff_switch: 0,
        staff_extract: 1,
        staff_extract_cash: 0.1
      },
      // 设置信息表单验证
      setFormRules: {
        limit: { validator: validateLimit, trigger: 'blur' },
        price: {
          required: true
        },
        recommend: {
          required: true
        },
        index_show: {
          required: true
        },
        is_self: {
          required: true
        },
        switch: {
          required: true
        },
        staff_switch: {
          required: true
        }
      },
      options: {},
      companyResult: [],
      isMemberAuth: false,
      levelList: [],
      memberForm: {
        is_member: 0,
        is_discount: 0,
        discount_data: []
      }
    }
  },
  async created () {
    setTimeout(() => {
      console.log(this.parimarOptions)
    }, 10000)
    let { id } = this.$route.query
    this.id = id
    this.navTitle = this.$t(id ? 'menu.GoodsEdit' : 'menu.GoodsAdd')
    await this.getMemberAuth()
    await this.getSelectOptions()
    await this.getDistribution()
    await this.getTemplateOptions()
    if (id) {
      await this.getGoodsInfo()
    }
  },
  methods: {
    async getMemberAuth () {
      let { code, data } = await this.$api.getBaseMemberAuth()
      if (code !== 200) return
      this.isMemberAuth = data
      if (!data) return
      let { code: rcode, data: rdata } = await this.$api.memberLevelSelect()
      if (rcode !== 200) return
      rdata.map(item => {
        item.discount = ''
      })
      this.levelList = rdata
    },
    changeParmar (id) {
      this.$api.getTemplateInfo({ template_id: id }).then(res => {
        this.parimarForm = res.data.info
        console.log(this.parimarForm)
      })
    },
    // 获取参数设置信息
    async getTemplateOptions () {
      let { code, data } = await this.$api.getGoodsTemplateOptions()
      if (code !== 200) return
      this.parimarOptions = data
    },
    /**
     * @method 获取分销设置
     */
    async getDistribution () {
      this.$api.getEditDistributionSetting().then(res => {
        console.log(res)
        if (res.code === 200) {
          this.distribution = res.data
        }
      })
    },
    /**
     * @method 获取商品详情
     */
    async getGoodsInfo () {
      this.$api
        .editGoodsInfo({
          id: this.id
        })
        .then(res => {
          console.log(res)
          if (res.code === 200) {
            // let {basicform, relevantForm, imgForm, setForm} = this
            let { data } = res

            if (data.memberForm) {
              for (let key in this.memberForm) {
                this.memberForm[key] = data.memberForm[key]
              }
              let arr = this.levelList
              arr.map(item => {
                data.memberForm.discount_data.map(aitem => {
                  if (item.id === aitem.member_id) {
                    item.discount = aitem.discount
                  }
                })
              })
              this.levelList = arr
            }

            let checkItem = []
            this.authList.map(item => {
              if (data.goods_info[item.key] === 1) {
                checkItem.push(item.title)
              }
            })
            this.sendForm.checkList = checkItem

            for (let key in this.relevantForm) {
              this.relevantForm[key] = data.goods_info[key]
            }

            this.imgForm = {
              cover: [
                {
                  url: data.goods_info.cover[0]
                }
              ],
              images: data.goods_info.images.map(item => {
                return {
                  url: item
                }
              }),
              image_url: data.goods_info.image_url,
              video: data.goods_info.video,
              video_cover: [
                {
                  url: data.goods_info.video_cover[0] || ''
                }
              ],
              content: data.goods_info.content
            }

            this.setForm = {
              all_company: data.goods_info.public_goods,
              is_limit: data.goods_info.is_limit,
              limit: data.goods_info.limit,
              start_min: data.goods_info.start_min,
              price: data.goods_info.price > 0 ? 1 : 0,
              recommend: data.goods_info.recommend,
              index_show: data.goods_info.index_show,
              is_send: data.goods_info.is_send,
              is_self: data.goods_info.is_self,
              switch: data.goods_info.switch || 0,
              extract: data.goods_info.extract || 1,
              extract_cash: data.goods_info.extract_cash || 0.1,
              staff_switch: data.goods_info.staff_switch || 0,
              staff_extract: data.goods_info.staff_extract || 1,
              staff_extract_cash: data.goods_info.staff_extract_cash || 0.1
            }

            let specIsImg = false
            data.spe_info.text.forEach(item => {
              item.inputVisible = false
              item.inputValue = ''
              if (item.is_img === 1) {
                specIsImg = true
                item.cate.map(aitem => {
                  if (aitem.image) {
                    aitem.image = [{ url: aitem.image }]
                  }
                })
              }
            })
            this.specIsImg = specIsImg
            this.specsForm.specsItem = data.spe_info.text
            this.copySpecsItem = data.spe_info.price
            this.specsForm.specsTable = data.spe_info.price
            let copyCompany = JSON.parse(JSON.stringify(data.company_goods))
            this.basicform = {
              template_id: data.goods_info.template_id,
              is_parameter: data.goods_info.is_parameter,
              name: data.goods_info.name,
              s_title: data.goods_info.s_title,
              type: data.goods_info.cate_reset_text,
              company_id:
                data.company_goods.length === this.companyResult.length
                  ? [[0], ...copyCompany]
                  : copyCompany,
              copyCompanyId:
                data.company_goods.length === this.companyResult.length
                  ? [[0], ...copyCompany]
                  : copyCompany
            }
            // 根据模板获取数据
            if (!data.goods_info.template_id) return
            this.changeParmar(data.goods_info.template_id)
          }
        })
    },
    /**
     * @method 生成表格
     */
    createTableData () {
      let { specsItem } = this.specsForm
      if (specsItem.length === 0) {
        this.specsForm.specsTable = []
      } else {
        let catArr = specsItem.map(item => {
          return item.cate
        })
        let testTableData = this.handleSpecsData(catArr)
        this.copySpecsItem.forEach(item => {
          testTableData.forEach(ele => {
            // eslint-disable-next-line eqeqeq
            if (item.id == ele.id) {
              ele.price = item.price
              ele.original_price = item.original_price
              ele.cost_price = item.cost_price
              ele.stock = item.stock
            }
          })
        })
        this.specsForm.specsTable = testTableData
      }
    },
    /**
     * @method 生成表格数据
     */
    handleSpecsData (arr) {
      if (arr.length > 1) {
        let len1 = arr[0].length
        let len2 = arr[1].length
        let newArr = arr.slice(0)
        let temp = []
        for (var i = 0; i < len1; i++) {
          for (var j = 0; j < len2; j++) {
            let id = arr[0][i].id + ',' + arr[1][j].id
            temp.push({
              id: id,
              title: arr[0][i].title + '-' + arr[1][j].title,
              price: '0.00',
              original_price: '0.00',
              cost_price: '0.00',
              stock: '0'
            })
          }
        }
        newArr.splice(0, 2, temp)
        return this.handleSpecsData(newArr)
      }
      return arr[0]
    },
    /**
     * @method 多选表格
     */
    handleSelectionChange (val) {
      this.specsForm.multipleSelection = val
    },
    getSelectOptions () {
      this.$api.GoodsListSelect().then(res => {
        if (res.code === 200) {
          let { data } = res
          this.handleCompanyOptions(data.company)
          data.company.length &&
            data.company.unshift({
              id: 0,
              name: '全选'
            })
          this.options = data
        }
      })
    },
    handleCompanyOptions (data, arr = []) {
      for (let i = 0, len = data.length; i < len; i++) {
        let newArr = JSON.parse(JSON.stringify(arr))
        newArr.push(data[i].id)
        this.companyResult.push(JSON.parse(JSON.stringify(newArr)))
        if (data[i].children && data[i].children.length) {
          this.handleCompanyOptions(data[i].children, newArr)
        }
      }
    },
    handleCloseTag (index, i) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.specsForm.specsItem[index].cate.splice(i, 1)
        this.$message.success(this.$t('tips.successDel'))
        this.createTableData()
      })
    },
    /**
     * @method 批量设置价格或者库存
     */
    setBatchPrice (type) {
      if (type === 2) {
        let { batchValue, dialogPriceTitle } = this
        let { multipleSelection } = this.specsForm
        let result = multipleSelection.map(item => {
          if (dialogPriceTitle === '原价') {
            item.original_price = batchValue
          } else if (dialogPriceTitle === '现价') {
            item.price = batchValue
          } else if (dialogPriceTitle === '成本') {
            item.cost_price = batchValue
          } else if (dialogPriceTitle === '库存') {
            item.stock = batchValue
          }
          return item
        })
        this.multipleSelection = result
      }
      this.batchValue = ''
      this.dialogPriceVisible = false
    },
    batchSetting (type) {
      this.dialogPriceTitle = type
      this.batchValue = 0
      this.dialogPriceVisible = true
    },
    /**
     * @method 删除规格名
     */
    delSpecsItem (index) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.specIsImg = this.specsForm.specsItem[index].is_img === 1 ? false : this.specIsImg
        this.specsForm.specsItem.splice(index, 1)
        this.$message.success(this.$t('tips.successDel'))
        this.createTableData()
      })
    },
    /**
     * @method 添加规格值
     */
    handleInputConfirm (index) {
      let { specsItem } = this.specsForm
      let { inputValue } = specsItem[index]
      inputValue = inputValue.replace(/^\s+|\s+$/g, '')
      if (!inputValue) {
        this.$message.error('请输入规格值')
        return
      }
      let hasSpecsValue = specsItem[index].cate.some(item => {
        return item.title === inputValue
      })
      if (hasSpecsValue) {
        this.$message.error('请勿添加相同的规格值！')
      } else {
        if (inputValue) {
          if (inputValue.length > 50) {
            this.$message.error('规格值最多只能输入50个字！')
          } else {
            this.specsForm.specsItem[index].cate.push({
              title: inputValue,
              id: Uuid(),
              price: '0.00',
              original_price: '0.00',
              cost_price: '0.00',
              stock: 0,
              image: ''
            })
            this.createTableData()
          }
        }
      }
      this.specsForm.specsItem[index].inputVisible = false
      this.specsForm.specsItem[index].inputValue = ''
    },
    showInput (index) {
      this.specsForm.specsItem[index].inputVisible = true
      this.$nextTick(_ => {
        this.$refs['saveTagInput' + index][0].$refs.input.focus()
      })
    },
    changeCheckBox (e) {
      this.authList.map(item => {
        this.setForm[item.key] = e.includes(item.title) ? 1 : 0
      })
    },
    /**
     * @method 返回上一页
     */
    goBack () {
      this.$route.meta.refresh = true
      this.$router.back(-1)
    },
    /**
     * @method 全选公司
     */
    choiceAllCompanyItem (val) {
      let { companyResult } = this
      // true = 添加  false 取消、减少
      let rType = val.length > this.basicform.copyCompanyId.length
      let firstVal = val[0] && val[0][0]
      if (rType) {
        // 添加
        if (firstVal && val.length === companyResult.length) {
          // 其他都选完了，就只有全选没选了
          this.basicform.company_id = [[0], ...val]
        } else if (firstVal === 0) {
          this.basicform.company_id = [[0], ...companyResult]
        }
      } else {
        // 减少
        if (firstVal === 0) {
          // 第一个为0 只取消全选
          let copyArr = JSON.parse(JSON.stringify(val))
          copyArr.splice(0, 1)
          this.basicform.company_id = copyArr
        } else if (val.length === companyResult.length) {
          // 清空
          this.basicform.company_id = []
        } else {
          this.basicform.company_id = val
        }
      }
      this.basicform.copyCompanyId = this.basicform.company_id
    },

    /**
     * @method 添加规格
     */
    addSpecsName (flag) {
      let specsName = JSON.parse(JSON.stringify(this.specsName)).replace(/^\s+|\s+$/g, '')
      if (flag === 2 && !specsName) {
        this.$message.error('请添加规格名！')
        return
      }
      if (flag === 2 && specsName) {
        let { specsItem } = this.specsForm
        let arr = specsItem.map(item => {
          return item.title
        })
        if (arr.indexOf(specsName) === -1) {
          if (this.dialogType === 'add') {
            this.specsForm.specsItem.push({
              inputVisible: false,
              inputValue: '',
              title: specsName,
              pid: Uuid(),
              cate: [],
              is_img: 0
            })
          } else {
            this.specsForm.specsItem[this.dialogIndex].title = specsName
          }
        } else {
          this.$message.error('请勿添加相同的规格名！')
        }
      }
      this.specsName = ''
      this.dialogIndex = ''
      this.dialogSwitch = false
    },

    /**
     * @method 添加或者修改规格名
     */
    addOrEditSpecsTitle (type, index) {
      if (type === 'add') {
        this.dialogTitle = '添加规格'
      } else if (type === 'edit') {
        this.dialogTitle = '编辑规格'
      }
      this.dialogSwitch = true
      this.dialogType = type
      this.specsName = this.specsForm.specsItem[index].title || ''
      if (index >= 0) this.dialogIndex = index
    },

    /**
     * @method 获取商品封面图
     */
    getGoodsCover (img) {
      this.imgForm.cover = img
    },

    /**
     * @method 设置规格图
     */
    getSpecImg (e, index, i) {
      console.log(e, index, i, '========getSpecImg')
      this.specsForm.specsItem[index].cate[i].image = e.length > 0 ? e : ''
    },
    setSpecImg (index) {
      if (!this.specIsImg) {
        this.specIsImg = true
        this.specsForm.specsItem[index].is_img = 1
        if (this.specsForm.specsItem[index].cate.length < 1) {
          this.$confirm(`请先添加规格值`,
            this.$t('tips.reminder'),
            {
              confirmButtonText: this.$t('action.comfirm'),
              cancelButtonText: this.$t('action.cancel'),
              type: 'warning'
            }
          ).then(() => {
            this.showInput(index)
          })
        }
      } else {
        if (this.specsForm.specsItem[index].is_img === 1) {
          this.specsForm.specsItem[index].is_img = 0
          this.specIsImg = false
        } else {
          this.$confirm(
            this.$t('tips.confirmDeleteSpecImage'),
            this.$t('tips.reminder'),
            {
              confirmButtonText: this.$t('action.comfirm'),
              cancelButtonText: this.$t('action.cancel'),
              type: 'warning'
            }
          )
            .then(() => {
              if (this.specsForm.specsItem[index].cate.length < 1) {
                this.$confirm(`请先添加规格值`,
                  this.$t('tips.reminder'),
                  {
                    confirmButtonText: this.$t('action.comfirm'),
                    cancelButtonText: this.$t('action.cancel'),
                    type: 'warning'
                  }
                ).then(() => {
                  this.showInput(index)
                })
              }

              let { specsItem } = this.specsForm
              specsItem.map((item, i) => {
                item.is_img = index === i ? 1 : 0
                if (index === i) {
                  item.cate.map(aitem => {
                    if (!aitem.image) return
                    aitem.image =
                      typeof aitem.image === 'string'
                        ? [{ url: aitem.image }]
                        : aitem.image
                    // aitem.image =
                    //   typeof aitem.image === 'string'
                    //     ? [{ url: aitem.image }]
                    //     : aitem.image
                  })
                }
              })
              this.specsForm.specsItem = specsItem
            })
            .catch(() => { })
        }
      }
    },
    /**
     * @method 获取商品轮播图
     */
    selectedFiles (imgs, key) {
      this.imgForm[key].push(...imgs)
    },
    moveFiles (imgs, key) {
      this.imgForm[key] = imgs
    },
    getVideo (video) {
      this.imgForm.video = video[video.length - 1].url
    },
    getVideoCover (img) {
      this.imgForm.video_cover = img
    },
    /**
     * @method 提交商品信息
     */
    submitGoodsInfo () {
      let { basicform, specsForm, relevantForm, imgForm, setForm, memberForm } = this
      let validObj = [
        {
          validTitle: 'basicform',
          isTrue: false
        },
        {
          validTitle: 'relevantForm',
          isTrue: false
        },
        {
          validTitle: 'imgForm',
          isTrue: false
        },
        {
          validTitle: 'setForm',
          isTrue: false
        },
        {
          validTitle: 'sendForm',
          isTrue: false
        }
      ]
      let allTrue = validObj.filter(item => {
        this.$refs[item.validTitle].validate(valid => {
          if (valid) {
            item.isTrue = true
          }
        })
        return item.isTrue
      })

      if (!specsForm.specsTable.length) {
        return this.$message.error('请输入规格值')
      }

      if (this.checkSpecsItems(specsForm.specsTable)) {
        return this.$message.error('请输入规格的价格和库存')
      }

      let discountMsg = ''
      if (this.isMemberAuth === true && this.memberForm.is_discount === 1) {
        this.levelList.map(item => {
          item.discount = item.discount || 10
          let reg = /^(([0-9]))(\.\d{1})?$/
          if (item.discount === 10 || item.discount === '10') {
            discountMsg = ''
          } else if (!reg.test(item.discount) || item.discount === '0.0') {
            discountMsg = '会员折扣取值范围为0-10，最多保留1位小数'
          }
        })
      }
      if (discountMsg) {
        return this.$message.error(discountMsg)
      }

      // let isSecondList = ''
      let isParmar = ''
      if (this.parimarForm && this.parimarForm.length > 0) {
        this.parimarForm.map(item => {
          if (!item.secondList[0].id) {
            item.secondList = []
            // isSecondList = true
            return true
          }
          if (item.secondList && item.secondList.length > 0) {
            item.secondList.map(vtem => {
              if (!vtem.parameter_value || !vtem.parameter_name) {
                isParmar = true
              }
            })
          }
        })
      }

      // if (isSecondList && this.basicform.is_parameter === 1) {
      //   return this.$message.error('该参数已删除!')
      // }
      if (isParmar && this.basicform.is_parameter === 1) {
        return this.$message.error('请输入参数值!')
      }

      if (allTrue.length === 5) {
        let queryObj = JSON.parse(
          JSON.stringify({
            basicform,
            specsForm,
            relevantForm,
            imgForm,
            setForm
          })
        )
        if (memberForm.is_discount === 1) {
          let arr = []
          this.levelList.map(item => {
            arr.push({ member_id: item.id, discount: item.discount })
          })
          memberForm.discount_data = arr
        }
        queryObj.memberForm = memberForm

        let errMsg = ''
        queryObj.specsForm.specsItem.map(item => {
          if (item.cate.length < 1) {
            errMsg = `规格名【${item.title}】暂未添加规格值，请先添加规格!`
          }
          if (item.is_img === 1) {
            item.cate.map(aitem => {
              if (!aitem.image) {
                errMsg = `规格名【${item.title}】暂未添加规格图片，请先添加规格图片!`
                return
              }
              aitem.image = aitem.image[0].url
            })
          }
        })

        if (errMsg) {
          this.$message.error(errMsg)
          return
        }

        queryObj.imgForm.cover = queryObj.imgForm.cover.map(item => {
          return item.url
        })
        queryObj.imgForm.images = queryObj.imgForm.images.map(item => {
          return item.url
        })
        queryObj.imgForm.video_cover = queryObj.imgForm.video_cover.map(
          item => {
            return item.url
          }
        )

        if (queryObj.basicform.company_id[0][0] === 0) {
          queryObj.basicform.company_id.splice(0, 1)
          queryObj.all_company = 1
        } else {
          queryObj.all_company = 0
        }
        if (queryObj.setForm.switch === 1 && !queryObj.setForm.extract) {
          queryObj.setForm.extract = 1
        }
        if (queryObj.setForm.switch === 2 && !queryObj.setForm.extract_cash) {
          queryObj.setForm.extract_cash = 0.1
        }
        if (
          queryObj.setForm.staff_switch === 1 &&
          !queryObj.setForm.staff_extract
        ) {
          queryObj.setForm.staff_extract = 1
        }
        if (
          queryObj.setForm.staff_switch === 2 &&
          !queryObj.setForm.staff_extract_cash
        ) {
          queryObj.setForm.staff_extract_cash = 0.1
        }
        queryObj.parameter = this.parimarForm
        if (this.id) {
          queryObj.id = this.id
          this.$api.editGoodsInfo(queryObj).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successSub'))
              this.goBack()
            }
          })
        } else {
          this.$api.addGoodsInfo(queryObj).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successSub'))
              this.goBack()
            }
          })
        }
      }
    },
    checkSpecsItems (data) {
      let result = data.some(item => {
        return (
          item.price === undefined ||
          item.price === '' ||
          item.stock === undefined ||
          item.stock === ''
        )
      })
      return result
    }
  },
  computed: {
    ...mapGetters(['adSwitch'])
  },
  watch: {
    'imgForm.content' (newValue, oldValue) {
      if (newValue && !oldValue) {
        this.$refs['imgForm'].clearValidate('content')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.parmai_view {
  display: flex;
  flex-wrap: wrap;
}
.lb-goods-edit {
  width: 100%;
  padding-bottom: 0px;
  position: relative;
  .el-form {
    margin: 20px 0;
  }

  .basic-form {
    .el-input {
      width: 300px;
    }

    .el-cascader {
      width: 300px;
    }

    .el-select {
      width: 300px;
    }
  }

  .relevant-form {
    padding-left: 80px;
    width: 1000px;
    .el-input {
      width: 160px;
    }
  }

  .img-form {
    .el-input {
      width: 300px;
    }
  }

  .set-form {
    .el-input {
      width: 300px;
      padding-right: -50px;

      i {
        font-style: normal;
        margin-right: 10px;
      }
    }
  }

  .specs-info {
    width: 925px;
    margin: 20px 0;

    .specs-item {
      width: 100%;
      display: flex;
      margin: 24px 0;

      .item-label {
        color: #606266;
        width: 180px;
        min-width: 180px;
        line-height: 35px;
        padding-right: 12px;
        text-align: right;

        .require {
          color: #f56c6c;
        }
      }

      .item-label-table {
        line-height: 50px;
      }

      .item-value {
        width: 100%;

        .table-bot {
          width: 100%;
          display: flex;
          align-items: center;

          span {
            margin-right: 20px;
          }

          .el-link {
            margin: 0 20px 0 0;
          }
        }
      }

      .item-value-specs-name {
        margin-bottom: 16px;

        .el-button {
          margin-left: 20px;
        }
      }

      .el-tag {
        margin: 5px 10px 5px 0px;
      }

      .input-new-tag {
        width: 90px;
        margin: 5px 10px 5px 0px;
        height: 28px;
        vertical-align: bottom;

        .el-input__inner {
          height: 28px !important;
        }
      }
    }
  }
}

.lb-input-number {
  width: 160px;
}

.dialog-inner {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;

  .el-input {
    width: 200px;
  }
}

.fiexd {
  height: 80px;
  position: fixed;
  background-color: rgb(255, 255, 255);
  box-shadow: rgb(240, 240, 240) 1px -2px 8px;
  bottom: 0px;
  left: 300px;
  z-index: 5;
  display: flex;
  padding-left: 100px;
  align-items: center;
}
.flex-warp {
  display: flex;
  flex-wrap: wrap;
}
.specsItem-tag-img {
  // display: inline-block;
  margin: 10px 10px 0 0;
}
</style>
